package com.hst.capacity.application.biz.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.hst.capacity.application.biz.AgentBiz;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.franchisee.AgentEntity;
import com.hst.capacity.domain.model.entity.setting.commision.AgentCommisionEntity;
import com.hst.capacity.domain.model.request.agent.AgentSaveParam;
import com.hst.capacity.domain.model.request.agent.AgentSearchParam;
import com.hst.capacity.domain.model.request.agent.AgentStatusParam;
import com.hst.capacity.domain.model.response.PagingResultVO;
import com.hst.capacity.domain.model.response.agent.AgentCommisionSaveParam;
import com.hst.capacity.domain.model.response.agent.AgentCommisionSelectVO;
import com.hst.capacity.domain.model.response.agent.AgentDriverNumberCountVO;
import com.hst.capacity.domain.model.response.agent.AgentSearchVO;
import com.hst.capacity.domain.service.AgentService;
import com.hst.capacity.domain.service.DriverService;
import com.hst.capacity.infra.convertor.AgentConvertor;
import com.hst.capacity.infra.persistence.jpa.agent.AgentInforRepository;
import com.hst.capacity.infra.persistence.jpa.setting.commision.AgentCommisionRepository;
import com.hst.capacity.infra.persistence.jpa.driver.DriverRepository;
import com.hst.capacity.infra.persistence.jpa.fleet.FleetInforRepository;
import com.hst.capacity.infra.persistence.mybatis.AgentMapper;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.NoSuchElementException;

@Service
public class AgentBizImpl implements AgentBiz {

    @Autowired
    private AgentInforRepository agentInforRepository;
    @Autowired
    private DriverRepository driverRepository;
    @Autowired
    private FleetInforRepository fleetInforRepository;
    @Autowired
    private AgentMapper agentMapper;
    @Autowired
    private DriverService driverService;
    @Autowired
    private AgentCommisionRepository commisionRepository;
    @Autowired
    private AgentService agentService;

    @Override
    public PagingResultVO<AgentSearchVO> search(AgentSearchParam param) {
        return new PagingResultVO<>(agentMapper.searchTotal(param),agentMapper.search(param));
    }

    @Override
    public String save(AgentSaveParam param) {
        boolean isExist = agentService.checkTel(param);
        if (isExist)
            throw new BusinessException(5002,"联系人电话已被使用，请更换！");
        return agentService.save(param).getIdAgent();
    }

    @Override
    public AgentSearchVO select(String idAgent) {
        AgentSearchVO re = AgentConvertor.INSTANCE.convertSelect(agentInforRepository.findById(idAgent).orElse(null));
        if (re==null)
            throw new BusinessException(5002,"运营商ID不存在,请检查!");
        return re;
    }

    @Override
    public Boolean statusOpen(AgentStatusParam param) {
        return this.statusToggle(param, Constants.AccountStatus.NORMAL);
    }

    @Override
    public Boolean statusFreeze(AgentStatusParam param) {
        return this.statusToggle(param, Constants.AccountStatus.FREEZE);
    }

    private boolean statusToggle(AgentStatusParam param, int status) {
        AgentEntity entity = agentInforRepository.findById(param.getIdAgent()).get();
        if (entity == null || entity.getIdFranchisee() == null) {
            throw new BusinessException(5002, "编辑对象不存在");
        } else {
            if (entity.getIdFranchisee() == null || !entity.getIdFranchisee().equals(param.getIdFranchisee()))
            throw new BusinessException(5002, "此服务商不属于此运力商");
        }
        entity.setStatus(status);
        return agentInforRepository.save(entity)!=null;
    }

    @Override
    public AgentDriverNumberCountVO driverAgentCount(String idAgent) {
        return driverService.driverAgentCount(idAgent);
    }

    @Override
    public List byFranchiseeList(String idFranchisee) {
        return AgentConvertor.INSTANCE.covert(agentInforRepository.findByIdFranchisee(idFranchisee));
    }

    @Override
    public AgentCommisionSelectVO commisionSelect(String idAgent) {
        return AgentConvertor.INSTANCE.covert(commisionRepository.findByIdAgent(idAgent));
    }

    @Override
    public String commisionSave(AgentCommisionSaveParam param) {
        AgentCommisionEntity entity;
        if (StrUtil.isNotEmpty(param.getIdAgentCommision())){
            entity=commisionRepository.findById(param.getIdAgentCommision()).orElse(null);
            if (entity==null)throw new BusinessException(5002,"编辑对象不存在");
            AgentConvertor.INSTANCE.covert(entity,param);
        }else {
            entity=newAgentCommisionEntity(param);
        }
        entity = commisionRepository.save(entity);
        return entity.getIdAgentCommision();
    }

    @Override
    public AgentCommisionSaveParam commisionSaveSelect(String idAgentCommision) {
        AgentCommisionSaveParam re = AgentConvertor.INSTANCE.covertSaveSelect(commisionRepository.findById(idAgentCommision).orElse(null));
        if (re==null)
            throw new BusinessException(5002,"编辑对象不存在");
        return re;
    }

    private AgentCommisionEntity newAgentCommisionEntity(AgentCommisionSaveParam param){
        AgentCommisionEntity agentCommisionEntity = new AgentCommisionEntity();
//        agentCommisionEntity.setIdAgentCommision(param.getIdAgentCommision());
        agentCommisionEntity.setIdAgent(param.getIdAgent());
        agentCommisionEntity.setIdFranchisee(param.getIdFranchisee());
        agentCommisionEntity.setModule(param.getModule());
        agentCommisionEntity.setCommisionType(param.getCommisionType());
        agentCommisionEntity.setAgentCommision(param.getAgentCommision());
        agentCommisionEntity.setMemo(param.getMemo());
//        agentCommisionEntity.setCreateBy();
//        agentCommisionEntity.setCreateTime();
//        agentCommisionEntity.setUpdateBy();
//        agentCommisionEntity.setUpdateTime();
        return agentCommisionEntity;
    }

    @Override
    public Boolean refreshAgentNumber(String idAgent) {
        AgentEntity ae = agentInforRepository.findById(idAgent).orElse(null);
        if (ae==null){
            return false;
        }
        int cnt_driver = driverRepository.agentDriverCount(idAgent);
        int cnt_fleet = fleetInforRepository.getFleetCountByAgent(idAgent);
        ae.setDriverNum(cnt_driver);
        ae.setFleetNum(cnt_fleet);
        agentInforRepository.save(ae);
        return true;
    }

}
